<!--  -->
<template>
  <div class="order">
    <div class="navheader borderbox">
      <img src="../assets/img/icon-back.png" class="back" @click="goBack" />
      <div class="title">我的订单</div>
    </div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      loading-text=""
    >
      <div class="order-item" v-for="item in order" :key="item.id">
        <div class="title">
          <div class="date">{{ changeTime(item.created_at) }}</div>
          <div class="status">{{ item.status_text }}</div>
        </div>
        <div class="body">
          <div class="goods">
            <div class="name">{{ item.goods[0].goods_name }}</div>
            <div class="payment">{{ item.payment_text }}</div>
          </div>
          <div class="charge"><span class="unit">￥</span>{{ item.charge }}</div>
        </div>
      </div>
    </van-list>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import Vue from "vue";
import { List } from "vant";

Vue.use(List);
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      order: [],
      loading: false,
      finished: false,
      page: 0,
      size: 10,
      total: 0,
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    // async onLoad() {
    //   // this.loading = false;
    //   let res = await this.$request.get("api/v2/member/orders", {
    //     params: {
    //       page: this.page + 1,
    //       page_size: this.size,
    //     },
    //   });
    //   this.total = res.data.total;
    //   this.order = [...this.order, ...res.data.data];

    //   this.loading = false;
    //   console.log(this.order.length);
    //   console.log(res.data.total);
    //   if (this.order.length >= res.data.total) {
    //     this.finished = true;
    //     console.log(111);
    //   }
    //   this.page = res.data.current_page;
    // },
    onLoad() {
      // this.$request
      //   .get("api/v2/member/orders", {
      //     params: {
      //       page: this.page + 1,
      //       page_size: this.size,
      //     },
      //   })
      //   .then((res) => {
      //     this.total = res.data.total;
      //     this.page = res.data.current_page;
      //     this.loading = false;
      //     this.order = [...this.order, ...res.data.data];
      //     if (this.order.length >= res.data.total) {
      //       this.finished = true;
      //     }
      //   });
      this.$request
        .get("api/v2/member/orders", {
          params: {
            page: this.page + 1,
            page_size: this.size,
          },
        })
        .then((res) => {
          this.total = res.data.total;
          // 0 表示没有更多的数据了
          if (res.data.total == 0) {
            this.finished = true;
          }
          this.page = res.data.current_page;
          this.loading = false;
          // 追加的逻辑
          this.order = [...this.order, ...res.data.data];
        });
    },
    friendlyDate(timestamp) {
      var formats = {
        year: "%n% 年前",
        month: "%n% 月前",
        day: "%n% 天前",
        hour: "%n% 小时前",
        minute: "%n% 分钟前",
        second: "%n% 秒前",
      };

      var now = Date.now();
      var seconds = Math.floor((now - timestamp) / 1000);
      var minutes = Math.floor(seconds / 60);
      var hours = Math.floor(minutes / 60);
      var days = Math.floor(hours / 24);
      var months = Math.floor(days / 30);
      var years = Math.floor(months / 12);

      var diffType = "";
      var diffValue = 0;
      if (years > 0) {
        diffType = "year";
        diffValue = years;
      } else {
        if (months > 0) {
          diffType = "month";
          diffValue = months;
        } else {
          if (days > 0) {
            diffType = "day";
            diffValue = days;
          } else {
            if (hours > 0) {
              diffType = "hour";
              diffValue = hours;
            } else {
              if (minutes > 0) {
                diffType = "minute";
                diffValue = minutes;
              } else {
                diffType = "second";
                diffValue = seconds === 0 ? (seconds = 1) : seconds;
                if (seconds <= 1) {
                  return "刚刚";
                }
              }
            }
          }
        }
      }
      return formats[diffType].replace("%n%", diffValue);
    },
    changeTime(time) {
      let date = new Date(time);
      let res = this.friendlyDate(date.getTime());
      return res;
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  async created() {
    // await this.$request
    //   .get("api/v2/member/orders", {
    //     page: "1",
    //     page_size: "10",
    //   })
    //   .then((res) => {
    //     console.log(res);
    //     this.order = res.data.data;
    //     console.log(this.order);
    //   })
    //   .catch((err) => {
    //     console.log(err);
    //   });
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="less" scoped>
.order {
  height: 100vh;
  .navheader.borderbox {
    position: fixed;
    top: 0;
    width: 100%;
    height: 50px;
    background-color: #fff;
    z-index: 100;
    padding-left: 15px;
    padding-right: 15px;
    display: flex;
    flex-direction: row;
    justify-content: center;
    border-bottom: 1px solid #f1f2f6;
    align-items: center;
    box-sizing: border-box;
    img.back {
      position: absolute;
      left: 15px;
      width: 19px;
      height: 19px;
    }

    .title {
      width: 72%;
      height: auto;
      font-size: 17px;
      font-weight: 500;
      text-align: center;
      color: #333;
      line-height: 22px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
  .order-item {
    width: 100%;
    height: auto;
    float: left;
    box-sizing: border-box;
    padding: 15px;
    background-color: #fff;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    .title {
      width: 100%;
      height: auto;
      float: left;
      display: flex;
      padding-bottom: 15px;
      .date {
        flex: 1;
        font-size: 10px;
        font-weight: 400;
        color: #999;
        line-height: 10px;
      }

      .status {
        flex: 1;
        text-align: right;
        font-size: 13px;
        font-weight: 400;
        color: #999;
        line-height: 13px;
      }
    }

    .body {
      width: 100%;
      height: auto;
      float: left;
      display: flex;
      .goods {
        flex: 1;
        .name {
          width: 100%;
          height: auto;
          float: left;
          font-size: 15px;
          font-weight: 400;
          color: #333;
          line-height: 15px;
          padding-bottom: 15px;
        }

        .payment {
          width: 100%;
          height: auto;
          float: left;
          font-size: 10px;
          font-weight: 400;
          color: #999;
          line-height: 10px;
        }
      }

      .charge {
        width: 120px;
        height: auto;
        text-align: right;
        font-size: 20px;
        font-weight: 400;
        color: #333;
        line-height: 20px;
        padding-top: 10px;
        span.unit {
          width: 120px;
          height: auto;
          text-align: right;
          font-size: 20px;
          font-weight: 400;
          color: #333;
          line-height: 20px;
          padding-top: 10px;
        }
      }
    }
  }
}
</style>
